
class UserController < ApplicationController

  TABLE_HEADINGS = [['Status', 'status'],
                    ['Name', 'name'],
                    ['User email', 'email',],
                    ['Created Date', 'created_at']                    ]
  
  # this uses the key from the SORT_MAP for the desired column to be the default, as well as providing which direction. DESC or ASC 
  DEFAULT_SORT = ['email', 'ASC']
                    
  # this maps the name of the column sorting param to the DB column. The columns without a table default to the model being queried
  SORT_MAP = {'status' => ['users.status', 'ASC'], 
              'name' => ['contact_infos.firstname', 'ASC'],
              'email' => ['users.email', 'ASC'],
              'created_at' => ['users.created_at', 'DESC']}

  INCLUDED_RELATIONS = [:contact_info]  
  
  SEARCH_OPTIONS = [['', ''],
                        ['Name', 'name'],
                        ['User email', 'email']]
  def list_users
    @headings = TABLE_HEADINGS
    get_sorted_objects(User, params, SORT_MAP, INCLUDED_RELATIONS, DEFAULT_SORT)    
  end
  
  SEARCH_MAP = {'email' => ['users.email'],
                'name' => ['contact_infos.firstname']}

  def search
    @search_options = SEARCH_OPTIONS
    @headings = TABLE_HEADINGS
    search_objects(User, params, SORT_MAP, INCLUDED_RELATIONS, DEFAULT_SORT, SEARCH_MAP)
  end
  
  def list_users_filtered 
    @search_options = SEARCH_OPTIONS
    @headings = TABLE_HEADINGS
    get_sorted_objects(User, params, SORT_MAP, INCLUDED_RELATIONS, DEFAULT_SORT, process_custom_params(params))    
  end

  def process_custom_params(params)
    if params[:status]
      conditions = 'users.status = ' + params[:status]
    end
    if params[:verified]
      verified = 'users.email_verified = ' + params[:verified]
      if conditions
        conditions += ' and ' + verified
      else
        conditions = verified
      end         
    end
    @extra_params = {:status => params[:status], :verified => params[:verified]}
    return conditions
  endend
